﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using HYView;
using ModelValidation.FireCompartment;

namespace ModelValidation.Commands
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	internal class CmdFireDoorFlipFacingChecking : IExternalCommand
	{
		public Result Execute(ExternalCommandData cmdData, ref string msg, ElementSet elems)
		{
			this.m_cmdData = cmdData;
			CmdViewType types = (CmdViewType)3;
			if (SwitchView.SetUsableView(types, cmdData) != Autodesk.Revit.UI.Result.Succeeded)
			{
				return Autodesk.Revit.UI.Result.Failed;
			}
			List<ViewPlan> lstViewPlan = new List<ViewPlan>();
			if (!FloorSelectManager.SelectFloors(this.Doc, ref lstViewPlan, "疏散层选择"))
			{
				return Autodesk.Revit.UI.Result.Succeeded;
			}
			ViewPlanElemTreeViewForm viewPlanElemTreeViewForm = new ViewPlanElemTreeViewForm(this.m_cmdData, lstViewPlan, ViewPlanElemTreeViewForm.ECheckType.eCheckFireDoorFlipFacing);
			if (viewPlanElemTreeViewForm.ShowDialog() == DialogResult.OK)
			{
				viewPlanElemTreeViewForm.ElemIDs.ForEach(new Action<ElementId>(this.FlipFireDoorFacing));
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		private void FlipFireDoorFacing(ElementId idDoor)
		{
			Transaction transaction = new Transaction(this.Doc, "FlipDoor");
			transaction.Start();
			try
			{
				(this.Doc.GetElement(idDoor) as FamilyInstance).flipFacing();
				transaction.Commit();
			}
			catch
			{
				transaction.RollBack();
			}
		}

		private Document Doc
		{
			get
			{
				return this.m_cmdData.Application.ActiveUIDocument.Document;
			}
		}

		private ExternalCommandData m_cmdData;
	}
}
